/**********************************************************************************
// DemoAirplane
// 
// Creation:	16 Jul 2007
// Updated:		27 May 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Test the polygon capabilities of the engine
//				Draw, move, translate and rotate an airplane-like polygon
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	


/**********************************************************************************/
/*                           AirplaneDemo Class                                   */
/**********************************************************************************/


class AirplaneDemo: public D3DGame
{
private:
	Poly plane;
	Sprite * demobox;
	Sprite * demobar;
	char text[256];

public:
	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutDown();
	void UnloadResources();
	void LoadResources();
};

/**********************************************************************************/

void AirplaneDemo::GameInit()
{
	Point vlist[24] = {Point(1,11),
                          Point(2,8),
						  Point(1,7),
						  Point(1,-1),
						  Point(3,-1),
						  Point(3,-2),
						  Point(11,-3),
						  Point(11,-6),
						  Point(3,-7),
						  Point(2,-8),
						  Point(1,-8),
						  Point(1,-7),
						  Point(-1,-7),
						  Point(-1,-8),
						  Point(-2,-8),
						  Point(-3,-7),
						  Point(-11,-6),
						  Point(-11,-3),
						  Point(-3,-2),
						  Point(-3,-1),
						  Point(-1,-1),
						  Point(-1,7),
						  Point(-2,8),
						  Point(-1, 11)};
	
	plane = Poly(vlist, 24);
	plane.MoveTo(GetWindowWidth()/2.0f, GetWindowHeight()/2.0f);
	plane.Scale(20.0f, 20.0f);

	LoadResources();
}

/**********************************************************************************/

void AirplaneDemo::LoadResources()
{
	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
	
	demobar = new Sprite("Resources/demoBar.png");
	demobar->MoveTo(GetWindowWidth()/2.0f, GetWindowHeight()-30.0f);
}

/**********************************************************************************/

void AirplaneDemo::UnloadResources()
{
	if (demobar) delete demobar;
	if (demobox) delete demobox;
}

/**********************************************************************************/

void AirplaneDemo::GameShutDown()
{
	UnloadResources();
}

/**********************************************************************************/

void AirplaneDemo::GameLogic()
{
	// for now test if user is hitting ESC and send WM_DESTROY
	EndsOnEscape();

	// Scale
	if (KeyDown('A')) // scale up
		plane.Scale(1.1f, 1.1f);
	else
	if (KeyDown('S')) // scale down
		plane.Scale(0.9f, 0.9f);

	// Rotate
	if (KeyDown('Z')) // rotate left
		plane.Rotate(-5);
	else
	if (KeyDown('X')) // rotate right
		plane.Rotate(5);

	// Translate
	if (KeyDown(VK_RIGHT)) // translate right
		plane.Translate(5,0);
	else
	if (KeyDown(VK_LEFT))  // translate left
		plane.Translate(-5,0);    

	if (KeyDown(VK_UP))    // translate up
		plane.Translate(0,-5); 
	else
	if (KeyDown(VK_DOWN))  // translate down
		plane.Translate(0,5);
} 

/**********************************************************************************/

void AirplaneDemo::GameDraw()
{
	// lock screen (for pixel plotting functions)
	LockScreen();
	plane.Draw();
	UnLockScreen();

	demobox->Draw();
	demobar->Draw();

	sprintf_s(text, "Airplane Demo");
	sysFont->Draw(55, 40, text);

	sprintf_s(text, "Polygon drawing and clippingg");
	sysFont->Draw(55, 80, text);

	sprintf_s(text, "Translation, rotation and scaling");
	sysFont->Draw(55, 100, text);

	sprintf_s(text, "Image position: %d x %d", plane.IntX(), plane.IntY());
	sysFont->Draw(55, 120, text);

	sprintf_s(text, "<A> and <S> Scale    <Z> and <X> - Rotate    <Arrows> - Translate");
	sysFont->Draw(0, GetWindowHeight()-37, text, Color(255,255,255), GetWindowWidth(), 30, DT_CENTER);
} 



/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	AirplaneDemo planeDemo;

	//planeDemo.SetFramePerSec(VARIABLEFPS);
	//planeDemo.SetWindowMode(WINDOWED);
	//planeDemo.SetWindowWidth(1024,768);
	planeDemo.SetWindowBgColor(10,25,55);
	planeDemo.SetWindowTitle(string("Airplane Demo"));
	planeDemo.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	planeDemo.SetWindowCursor(LoadCursor(hInstance, MAKEINTRESOURCE(IDI_CURSOR)));
	planeDemo.ShowWindowCursor(false);
	planeDemo.Initialize(hInstance);
	planeDemo.Start();
	
	return 0;

} 

/**********************************************************************************/

